//
//  FangYuanCell.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/8.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "FangYuanCell.h"
#import "UIButton+WebCache.h"
#import "StringTool.h"
#import "API.h"

#import "PhotoShowManage.h"

@interface FangYuanCell()

@property(nonatomic,strong) NSArray *array;
@end

@implementation FangYuanCell

+(instancetype)cellWithTableView:(UITableView *)tableView WithFangXingPicsArr:(NSArray *)array
{
        FangYuanCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"FangYuanCell" owner:nil options:nil]lastObject];
       cell.array = array;
        if (array.count  != 0 && array != nil)
        {
            for (int i = 0; i < array.count; i ++)
            {
                [cell initImgeViewWithIndex:i WithUrl:[array objectAtIndex:i] withCell:cell];
            }

        }
    
   
    [cell  setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


-(void)initImgeViewWithIndex:(NSInteger)index WithUrl:(NSString *)url withCell:(FangYuanCell *)cell
{
    CGFloat imageX = 10;
    CGFloat imageW = UIScreenWidth-2*imageX;
    CGFloat imageH = 260;
    CGFloat margin = 10;
    CGFloat imageY = 42+index*imageH + margin*(index+1);
    
    NSString *imageurl = nil;
    if (![StringTool isBlankString:url]) {
        imageurl = [API imgUrlMake:url];
    }
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
    [btn sd_setBackgroundImageWithURL:[NSURL URLWithString:imageurl] forState:UIControlStateNormal placeholderImage:nil];
    btn.tag = [self.array indexOfObject:url];
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
    
}

+(CGFloat)getHeightWithTableView:(UITableView *)tableView WithFangYuanPicsArr:(NSArray *)array
{
    
    
    CGFloat imageH = 260;
    CGFloat margin = 10;
    if (array.count  != 0 && array != nil)
    {
        return 42+array.count*imageH + (array.count+1)*margin;
    }

    return 42;
    
}

-(void)btnOnClick:(UIButton*)sender{
    
    if(self.array.count>0){
        MWPhotoBrowser *br = [PhotoShowManage  getMWPhotoShowInstance:self];
        [br setCurrentPhotoIndex:sender.tag];
        [self.bufNvc pushViewController:br animated:YES];
    }
}

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.array.count;
}


- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    
    //   return  [MWPhoto photoWithURL:[NSURL URLWithString:headData.mainImgUrl]];
    
    if (index < self.array.count)
    {
        NSString *url =  [API imgUrlMake:[self.array objectAtIndex:index]];;
        
        return  [MWPhoto photoWithURL:[NSURL URLWithString:url]];
    }
    
    return nil;
    
}


@end
